package edu.towson.cosc.cosc455.kchmura.compiler;

import java.io.*;
import java.util.ArrayList;

/**
 *
 * @author kchmur1
 * This contains the main method which runs the program.
 */
public class compiler {

    /**
     * Initialize class variables.
     */
    static String token;
    static MyLexicalAnalyzer lexer;
    static MySyntaxAnalyzer parser;
    static MySemanticAnalyzer generator;
    static String extension = "lol";
    static ArrayList<String> tokenArray;
    static BufferedWriter out;
    static BufferedReader reader = null;
    static String lineFromFile = null;

    /**
     * Main method.
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        token = " ";
        
        String inputFile = args[0];

        /**
         * Checks for valid argument length.
         */
        if (args.length != 1) {
            System.err.println("Command Line Argument Does Not Match Required Length (1)");
            System.exit(0);
        }

        /**
         * Splits the input file name to check for extension.
         */
        String[] splitCheck = inputFile.split("\\.(?=[^\\.]+$)");

        /**
         * Checks for a valid extension on the input file.
         */
        if (!splitCheck[1].equals(extension)) {
            System.err.println("Input File Extension Does Not Match Required Input Extension");
            System.exit(0);
        }

        
        /**
         * Prepares to read in the file.
         */

        BufferedReader bufferedReader = new BufferedReader(new FileReader(inputFile));
        StringBuffer stringBuffer = new StringBuffer();
        
 
        while((lineFromFile =bufferedReader.readLine())!=null){
 
            stringBuffer.append(lineFromFile).append("\n");
        }
   
        //tester for reading in the file
        System.out.println(stringBuffer);
        
 
        bufferedReader.close();
       
       
        /**
         * Creates output file.
         */
        try {
            File outputFile = new File("lolcodeOutput.lol");
            out = new BufferedWriter(new FileWriter(outputFile));
        } catch (IOException e) {
        }

        lexer = new MyLexicalAnalyzer();
        parser = new MySyntaxAnalyzer();
        generator = new MySemanticAnalyzer();

        lexer.getNextToken();
        parser.lolcode();
        generator.semanticConverter();

        out.close();
    }

    /**
     * This method adds the tokens to the tokenArray.
     *
     * @param token
     */
    public static void addToArrayMethod(String token) {
        for (int j = 0; j < tokenArray.size(); j++) {
            System.out.println(token);
            tokenArray.add(token);
        }

    }

    /**
     * This method writes the tokens to the output file and removes the token
     * from tokenArray.
     *
     * @param token
     * @throws IOException
     */
    public static void outputArrayMethod(String token) throws IOException {
        for (int j = 0; j < tokenArray.size(); j++) {
            System.out.println(token);
            out.write(token);
            tokenArray.remove(token);
        }

    }
}
